package com.action.background;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;


import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.util.IPUtil;

public class GuanLiCenterAction {
	private String path;
	private File file;
	
	//默认图片
	public String getMoRenUserImg(){
		
		Map<String, Object> request= ActionContext.getContext().getContextMap();
		String realPath = IPUtil.getFilePath()+"/img/uersImg/picturesImg/MoRenUserImg/";
		System.out.println(realPath);
		File f = new File(realPath);
		String[] list = f.list();
		request.put("imgList", list);
		return "getMoRenUserImg";
	}
	
	//删除默认图片
	public String deleteMoRenImg(){
		String realPath = IPUtil.getFilePath()+"/img/uersImg/picturesImg/MoRenUserImg/";
		File f = new File(realPath+"\\"+path);
		f.delete();
		return "deleteMoRenImg";
	}
	
	//添加图片
	public String addMoRenImg(){
		String realPath = IPUtil.getFilePath()+"/img/uersImg/picturesImg/MoRenUserImg/";
		try {
			
			String FileName = fileName()+".png";//创建文件名
			File fi = new File(realPath,FileName);
			FileUtils.copyFile(file,fi);
		} catch (IOException e) {
			e.printStackTrace();
			return "err";
		}
		return "addMoRenImg";
	}
	
	private static String fileName(){
		String realPath = IPUtil.getFilePath()+"/img/uersImg/picturesImg/MoRenUserImg/";
		System.out.println(realPath);
		File f = new File(realPath);
		String[] l = f.list();
		int charAt = 0;
		if(l.length!=0){
			System.out.println(l[l.length-1].charAt(0));
			charAt = (Integer.parseInt(l[l.length-1].charAt(0)+""))+1;
			System.out.println("charAt="+charAt);
		}else{
			charAt = 1;
		}
		String FileName = charAt+"MO";
		SimpleDateFormat sdf = new SimpleDateFormat("yyMMddHHmmss");
		FileName = FileName +sdf.format(new Date());
		return FileName;
	}
	//修改图片
	public String updateMoRenImg(){
		String realPath = IPUtil.getFilePath()+"/img/uersImg/picturesImg/MoRenUserImg/";
		String FileName= path.charAt(0)+"MO";
		SimpleDateFormat sdf = new SimpleDateFormat("yyMMddHHmmss");
		FileName = FileName +sdf.format(new Date())+".png";
		
		File fi = new File(realPath+"\\"+path);
		fi.delete();
		
		File fil = new File(realPath, FileName);
		
		try {
			FileUtils.copyFile(file,fil);
		} catch (IOException e) {
			e.printStackTrace();
			return "err";
		}
		return "updateMoRenImg";
	}
	
	//头像
	public String getMORenHeadImg(){
		Map<String, Object> request = ActionContext.getContext().getContextMap();
		String r = IPUtil.getFilePath()+"/img/uersImg/headImg/MoRenHead/";
		System.out.println("默认头像"+r);
		File  f = new File(r);
		String[] list = f.list();
		request.put("moren", list[0]);
		return "getMORenHeadImg";
	}
	
	//修改头像
	public String updateMoRenHeadImg(){
		String r = IPUtil.getFilePath()+"/img/uersImg/headImg/MoRenHead/";
		System.out.println(r);
		File  f = new File(r);
		String[] l = f.list();
		File f1 = new File(r, l[0]);
		f1.delete();
		SimpleDateFormat sdf = new SimpleDateFormat("yyMMddHHmmss");
		String FileName = "MOHEAD" +sdf.format(new Date())+".png";
		File fi = new File(r,FileName);
		try {
			FileUtils.copyFile(file,fi);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return "updateMoRenHeadImg";
	}
	
	public void setPath(String path) {
		this.path = path;
	}
	public File getFile() {
		return file;
	}
	public void setFile(File file) {
		this.file = file;
	}
}
